import {request} from '../../utils/http'
let app = getApp()
let timer = null

Page({
  data: {
    btnCaptchaText:'获取验证码',
    logo:'',
    mobile:'',
    captcha:'',
    btnCaptchaState:true,
    currentTime:60,
    member:{},
    checked:false
  },
  onLoad() {
    console.log(app.globalData.logo)
    
    request('/shop/getSwitchConfigs','get').then(res => {
      if (res.code === 200) {
        this.setData({
          logo:res.data.app_logo
        })
      }
    })
    
  },
  inputMobile(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  inputCaptcha(e){
    this.setData({
      captcha:e.detail.value
    })
  },
  sendCaptcha(){
    if (!this.data.btnCaptchaState) {
      return
    }

    let mobile = this.data.mobile;
    if (!mobile) {
      my.showToast({content: '请输入手机号码'})
      return
    }

    if (!/^1[0-9]\d{9}$/.test(mobile)) {
      my.showToast({content: '手机号码格式不对！'})
      return;
    }

    this.setData({
      btnCaptchaState:false
    })

    request('/send_sms', 'post', {
      mobile
    }).then(res => {
      if (res.code === 200 ) {
        my.showToast({content: '发送成功'})

        timer = setInterval(() => {
          if (this.data.currentTime == 0) {
            clearInterval(timer)
            timer = null
            this.setData({
              btnCaptchaState: true,
              btnCaptchaText:'获取验证码',
              currentTime:60
            })
          }  else {
            this.setData({
              btnCaptchaText: this.data.currentTime + 's'
            })
          }

          this.setData({
            currentTime: this.data.currentTime - 1
          })
        },1000)
      } else {
        my.showToast({content: res.message})

      }
      
    })

  },
  getPhoneNumber(){
   
  },
  submit(){
    let mobile = this.data.mobile;
    if (!mobile) {
      my.showToast({content: '请输入手机号码'})
      return
    }

    if (!/^1[0-9]\d{9}$/.test(mobile)) {
      my.showToast({content: '手机号码格式不对！'})
      return;
    }

    let captcha = this.data.captcha

    if (!captcha) {
      my.showToast({content: '请输入验证码'})
      return
    }

    if (!this.data.checked) {
      my.showToast({content: '请同意用户协议隐私条款'})
      return
    }

    my.getAuthCode({
      scopes: 'auth_base',
      success: ({ authCode }) => {
        request('/sms_login', 'post', {
            mobile,
            smsCode:captcha,
            authCode
        }).then(res => {
          if (res.code === 200) {
            this.setData({
              member: {
                ...res.data
              }
            })
    
            my.setStorage({
              key: 'userInfo',
              data: {
                ...res.data
              },
              success: function() {
                console.log('缓存写入成功')
              }
            })
    
            my.navigateBack();
          } else {
            my.showToast({content: res.message})

          }
        })
      }
    })

    
  },
  handleChange(e){
    console.log(e)
    this.setData({
      checked:e.detail.value
    })
  },
  goAgreement(){
    my.navigateTo({
      url: '/pages/agreement/agreement'
    });
  }
});
